package com.example.blackbox.Activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import androidx.annotation.Nullable;

import com.example.blackbox.R;

public class RegistereActivity extends Activity {
    EditText username,password,confirmPassword;
    Button registere;
    SharedPreferences sp;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registered);
        initUI();

        registere.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String pw="";
                String pwd="";
                pw=password.getText().toString();
                pwd=confirmPassword.getText().toString();
                if ( pw.equals(pwd)&&!username.getText().toString().equals("")){
                    sp.edit().putString("username",username.getText().toString()).commit();
                    sp.edit().putString("password",password.getText().toString()).commit();
                    Toast.makeText(RegistereActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    //设置广播
                    Intent intent=new Intent();
                    intent.setAction("SIGN_INFO_ACTION");
                    intent.putExtra("username",username.getText().toString());
                    intent.putExtra("password",pw);
                    sendBroadcast(intent);
                }else{
                    Toast.makeText(RegistereActivity.this,"请验证用户名和密码",Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    public void initUI(){
        username=(EditText)findViewById(R.id.registere_et_username);
        password=(EditText)findViewById(R.id.registere_et_password);
        confirmPassword=(EditText)findViewById(R.id.registere_et_determine_password);
        registere=(Button) findViewById(R.id.registere_bt_registere);
        sp=getSharedPreferences("userinfo", Context.MODE_PRIVATE);
    }
}
